package ru.paswd.demo.android.view.ui.activities

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.View.GONE
import android.view.View.OnClickListener
import android.view.View.VISIBLE
import android.widget.FrameLayout
import android.widget.TextView
import android.widget.Toast
import dagger.hilt.android.AndroidEntryPoint
import moxy.MvpAppCompatActivity
import moxy.presenter.InjectPresenter
import moxy.presenter.ProvidePresenter
import ru.paswd.demo.android.view.R
import ru.paswd.demo.android.view.components.HistoryStorage
import ru.paswd.demo.android.view.components.SqEquResolver
import ru.paswd.demo.android.view.presenters.MainPresenter
import ru.paswd.demo.android.view.ui.views.MainView
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : MvpAppCompatActivity(R.layout.activity_main), MainView, OnClickListener {

    @InjectPresenter
    lateinit var presenter: MainPresenter

    @Inject
    lateinit var sqEquResolver: SqEquResolver

    @Inject
    lateinit var historyStorage: HistoryStorage

    @ProvidePresenter
    fun providePresenter() =
        MainPresenter(
            resolver = sqEquResolver,
            historyStorage = historyStorage,
        )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val countButton = findViewById<View>(R.id.countButton)
        val historyButton = findViewById<View>(R.id.historyButton)
        countButton.setOnClickListener(this)
        historyButton.setOnClickListener(this)
    }

    override fun onClick(view: View?) {
        view ?: return

        when (view.id) {
            R.id.countButton -> onCalculateClicked()
            R.id.historyButton -> presenter.onHistoryClicked()
        }
    }

    private fun onCalculateClicked() {
        val viewArgA = findViewById<TextView>(R.id.argA)
        val viewArgB = findViewById<TextView>(R.id.argB)
        val viewArgC = findViewById<TextView>(R.id.argC)

        val argA = readArg("A", viewArgA) ?: return
        val argB = readArg("B", viewArgB) ?: return
        val argC = readArg("C", viewArgC) ?: return

        presenter.onCalculateClicked(argA, argB, argC)
    }

    private fun readArg(argName: String, view: TextView): Int? =
        try {
            view.text.toString().toInt()
        } catch (ex: Exception) {
            Toast.makeText(
                this,
                "Невозможно прочитать аргумент $argName", Toast.LENGTH_SHORT
            ).show()
            null
        }

    override fun reset() {
        modifyResult { countResult, countResultLayout ->
            countResultLayout.visibility = GONE
            countResult.text = ""
        }
    }

    override fun displayResults(results: List<Double>) {
        modifyResult { countResult, countResultLayout ->
            countResult.text = results.joinToString(" | ")
                .takeIf { it.isNotEmpty() }
                ?: "-"
            countResultLayout.visibility = VISIBLE
        }
    }

    override fun displayHistory() {
        val intent = Intent(this, HistoryActivity::class.java)
        startActivity(intent)
    }

    private fun modifyResult(
        consumer: (countResult: TextView, countResultLayout: FrameLayout) -> Unit
    ) {
        val countResult = findViewById<TextView>(R.id.countResult)
        val countResultLayout = findViewById<FrameLayout>(R.id.countResultLayout)
        consumer.invoke(countResult, countResultLayout)
    }
}